package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.car.actionform.CarStockModifyForm;
import com.tianqu.tms.www.car.controller.CarStockModifyController;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class CarStockModifyAction extends TmsAction{
	public static final String DETAIL = "detail";
	private Logger logger = Logger.getLogger(CarStockCountListTopAction.class);
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
		CarStockModifyForm carStockModifyForm = (CarStockModifyForm) uc.getInputForm();
		if(StringUtil.isNullOrBlank(carStockModifyForm.getIndex())) {
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		//
		if(StringUtil.isNullOrBlank(carStockModifyForm.getId())){
			if (StringUtil.isNullOrBlank(carStockModifyForm.getVin()) && StringUtil.isNullOrBlank(carStockModifyForm.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return mapping.findForward(TmsConstants.SUCCESS);
			}

			if (!StringUtil.isNullOrBlank(carStockModifyForm.getVin()) && !StringUtil.isNullOrBlank(carStockModifyForm.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return mapping.findForward(TmsConstants.SUCCESS);
			}

			//车牌5位check
			if (!StringUtil.isNullOrBlank(carStockModifyForm.getLicensePlate()) && carStockModifyForm.getLicensePlate().length() !=5) {
				setErrorsToRequest(request, TmsConstants.U8009, null); // 请输入车牌号码后5位。modify by mufeiyu
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return mapping.findForward(TmsConstants.SUCCESS);
			}
			//vin 6位check
			if (!StringUtil.isNullOrBlank(carStockModifyForm.getVin()) && carStockModifyForm.getVin().length() !=6) {
				setErrorsToRequest(request, TmsConstants.U8010, null); // 请输入车架号后6位。modify by mufeiyu
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return mapping.findForward(TmsConstants.SUCCESS);
			}
		}
		

		//
		CarStockModifyController controller = new CarStockModifyController();
		controller.execute(uc);
		
		//
		viewhelper = (CarStockModifyViewhelper) uc.getAttribute(TmsConstants.VIEWHELP);
		if (viewhelper.getConfirmFlag() == 1) {
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			setErrorsToRequest(request, TmsConstants.U0039, null);
		}else if(viewhelper.getConfirmFlag() == 2){
			Codes code = Codes.getInstance();
			Member menber = Member.getInstance();
			viewhelper.setCardTypeList(code.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
			viewhelper.setMemberList(menber.findAllMemInfo(1));// 会员类型  modify by huangwei st2
			viewhelper.setGearBoxList(code.getCodeslistByItemId(Codes.GEARBOX_TYPE));//变速箱类型list
			viewhelper.setColorList(code.getCodeslistByItemId(Codes.VEHICLE_COLOR));// 车辆颜色
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			return mapping.findForward(DETAIL);
		}else{
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		}
	
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
